package com.hndzyu.yd.vip;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.bean.XWAdapterBean;
import com.hndzyu.yd.utils.Util;

import java.util.List;

/**
 * Created by zz9527 on 2018/12/5.
 */
public class XWDetailAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private List<XWAdapterBean> mList;
    private final Context context;

    public XWDetailAdapter(Context context, List<XWAdapterBean> list) {
        this.context = context;
        this.mList = list;
        inflater = LayoutInflater.from(context);
    }

    public void setData(List<XWAdapterBean> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public XWAdapterBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.lv_item_task_desc, null);
            viewHolder.tv_id = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_id);
            viewHolder.tv_desc = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_desc);
            viewHolder.tv_status = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_status);
            viewHolder.tv_points = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_points);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        XWAdapterBean bean = mList.get(position);

        // 设置左边的数字序号
        viewHolder.tv_id.setText(String.valueOf(position + 1));
        // 设置任务赚取描述
        try {
            viewHolder.tv_desc.setText(bean.getEvent());

        } catch (Exception e) {
        }
        // 设置积分或者已完成
        if (bean.getEvent().contains("充值")){
            if (position==0){
                if (UserBean.level > 0) {
                    viewHolder.tv_points.setText("+" + Util.Sub((bean.getMoney()+1) * (Double.parseDouble(UserBean.vipdivide) + Double.parseDouble(UserBean.divide)) + "") + "元");
                } else {
                    viewHolder.tv_points.setText("+" + Util.Sub((bean.getMoney()+1) * Double.parseDouble(UserBean.divide) + "") + "元");

                }
            }else {
                if (UserBean.level > 0) {
                    viewHolder.tv_points.setText("+" + Util.Sub(bean.getMoney()/0.625 * (Double.parseDouble(UserBean.vipdivide) + Double.parseDouble(UserBean.divide)) + "") + "元");
                } else {
                    viewHolder.tv_points.setText("+" + Util.Sub(bean.getMoney()/0.625 * Double.parseDouble(UserBean.divide) + "") + "元");

                }
            }
        }else {
            if (UserBean.level > 0) {
                viewHolder.tv_points.setText("+" + Util.Sub(bean.getMoney() * (Double.parseDouble(UserBean.vipdivide) + Double.parseDouble(UserBean.divide)) + "") + "元");
            } else {
                viewHolder.tv_points.setText("+" + Util.Sub(bean.getMoney() * Double.parseDouble(UserBean.divide) + "") + "元");

            }
        }
//        Drawable rightDrawable = context.getResources().getDrawable(R.mipmap.share);
//        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
//        viewHolder.tv_points.setCompoundDrawables(rightDrawable, null, null, null);
//        viewHolder.tv_points.setBackgroundResource(R.drawable.bg_money);
//        viewHolder.tv_points.setTextColor(context.getResources().getColor(R.color.ff9d0f));
        viewHolder.tv_status.setText(bean.getProgress());
        if (bean.getProgress().contains("进行")){
            viewHolder.tv_points.setTextColor(context.getResources().getColor(R.color.ff9d0f));
        }else {
            viewHolder.tv_points.setTextColor(context.getResources().getColor(R.color.red));
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView tv_id;
        TextView tv_desc;
        TextView tv_status;
        TextView tv_points;
    }
}
